from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from utils.response import JsResponse, UnicornException, NotAuthorizedException
from app.api.wechat_controller import wechat_router

app = FastAPI(default_response_class=JsResponse, docs_url=None, redoc_url=None,
              openapi_url=None)  # create an app instance


# 自定义异常处理器
@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
    return JSONResponse(status_code=200, content={'status': exc.status, 'msg': exc.msg, 'data': exc.data})


@app.exception_handler(NotAuthorizedException)
async def not_authorized_exception_handler(request: Request, exc: NotAuthorizedException):
    return JSONResponse(status_code=403, content={'status': exc.status, 'msg': exc.msg, 'data': exc.data})


@app.get('/')
async def welcome() -> dict:
    return {"message": "Welcome to my Pagaaae"}


app.include_router(wechat_router)

if __name__ == "__main__":
    import uvicorn

    # uvicorn.run(app, host="0.0.0.0", port=8041)
    uvicorn.run(app, host="0.0.0.0", port=8042)
